// This file define the database interface to historic DB
// Copyright (c) 2006-, ASAT Technology Inc.
// Designed by Robin and Jack
#ifndef _DAP_SQLDB_H_
#define _DAP_SQLDB_H_

#include "DAPSystem.h"
#include "DAPEvent.h"
class CDAPDBDriverObj;
class CDAPSQLObj;
class AFX_EXT_CLASS CDAPDatabaseSQLInterface : public CDAPObj
{
// Constructor
public:
	CDAPDatabaseSQLInterface();
	~CDAPDatabaseSQLInterface();
// Attribute
public:
	/////////////////////////////////////////////////////////////////
	CDAPDBDriverObj		*m_pMySQLObj;		// Using MYSQL driver 

	/////////////////////////////////////////////////////////////////
	// Items waited to be logged
	CDAPLogTrendingItemList	m_lstTrend;		// Historic trending items
	CCriticalSection		m_rwTrend;	
	CDAPEventList			m_lstEvent;		// Event item to be logged
	CCriticalSection		m_rwEvent;	
	CCriticalSection		m_rwSQL;	
	CTypedPtrList<CObList,CDAPSQLObj *>	m_lstWaitedSQL;
// Helper
public:
	bool IsConnected();
	// General interface to exicute SQL 
	bool ExecuteSQL(CDAPString szSQL, CDAPString &szError, bool bDirectFlag=false);
	// Add the SQL to SQL buffer
	// Push an event to the event queuqe
	bool AddLogEvent(CDAPEvent *pEvent);
	// Push an trending item to the trending queuqe
	bool AddLogTrending(CDAPLogTrendingItem *pLogItem);
	// Set Available Diskspace
	void SetAvailableDiskSpce(float fSpaceRatio);

// Operation
public:
	// 1. Initialize the SQLDatabase
	//    1.1 Read configuration parameters from the configuration files, maximum record for COS, SOE, ....,..
	//    1.2 Check create the system tables, USR, STS, COS, SOE, ....,...
	bool Initialize(CDAPInterface *pCDPPSystem);
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 2. Add check tag configuration
	//
	//    2.1. Insert a record to the tag configuration table
	//	  2.2  If recovery from NVRAM is configurated, try to check if the record in the STS table is matched, if not recreate it
	//    2.3     when pTag==NULL, Delete those records in the STS table which are not used by current configuration
	bool AddTagConfigRecord(CDAPString szRSShowName,CDAPTag *pTag);
	bool AddIEDConfigRecord(RSID nRSID, MGID nMGID);
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 3. Start the working thread for the SQL database
	//  
	bool Start();
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 4. Stop the SQL dabase working thread
	bool Stop();
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 5. Retrieve last value of a given tag from the STS table, is tag not defined, return false
	// 
	bool GetTagValue(GLPTID nTagID, DAPValueType nValueType, DAPValue &uValue);
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 6. Update Tag Value
	//
	bool UpdateTagValue(GLPTID nTagID, DAPValueType nValueType, DAPValue uValue,/*modify by wang*/bool IsEMP=false);
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 7. Retrieve last confirmed value for the given AO tag
	//
	bool GetTagConfirmedValue(GLPTID nTagID, DAPValueType nValueType, DAPValue &uValue);
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 8. Update Tag Value
	//
	bool UpdateTagConfirmedValue(GLPTID nTagID, DAPValueType nValueType, DAPValue uValue);

	//////////////////////////////////////////////////////////////////////////
	// Add Robin Zhao
	// 2006.3.25
	//////////////////////////////////////////////////////////////////////////
	// 9. Connect
	//
	bool Connect(CDAPString szMachineB="", bool bRemote=false);
	// 10. Initialize historic table list
	//
	bool LoadTables();
	// 11. Clear tables
	//
	bool ClearTables(unsigned char nType);
	// 12. Query station device name
	bool GetDeviceName(CDAPString szStationName, unsigned short wDeviceID,CDAPString &szDeviceName);
	// 13. Fetch Multi-value
	bool FetchMultValue(CDAPString szSQL,int nColCount,CDAPStringArray &arrValue);
	// Add 2009.1.21
	bool PrepareDataTable();

};
#endif